package id.co.rho.scoring.cid.client.widgets;

import id.co.rho.scoring.cid.client.model.ChangeTrackingModel;

import java.math.BigDecimal;
import java.util.List;

import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.i18n.client.NumberFormat;

public class WidgetBuildHelper {

	public WidgetBuildHelper() {
		super();
	}
	
	public DirtyAwareTextField<String> constructTextField(String name, String label, int maxLength){
		DirtyAwareTextField<String> newTextField = new DirtyAwareTextField<String>();
		newTextField.setName(name);
		newTextField.setFieldLabel(label);
		newTextField.setMaxLength(maxLength);
		return newTextField;
	}
	
	public DirtyAwareTextField<Integer> constructTextIntField(String name, String label, int maxLength){
		DirtyAwareTextField<Integer> newTextField = new DirtyAwareTextField<Integer>();
		newTextField.setName(name);
		newTextField.setFieldLabel(label);
		newTextField.setMaxLength(maxLength);
		return newTextField;
	}

    public DirtyAwareTextFieldNButton<String> constructTextFieldNBUtton(String name, String label, int maxLength, Button button){
		DirtyAwareTextFieldNButton<String> newTextField = new DirtyAwareTextFieldNButton<String>(button);
		newTextField.setName(name);
		newTextField.setFieldLabel(label);
		newTextField.setMaxLength(maxLength);
		return newTextField;
	}
    
    public DirtyAwareComboBox constructComboBoxParam(String name, String label, List content){
    	ListStore store = new ListStore();
    	store.add(content);
    	
    	DirtyAwareComboBox newComboBox = new DirtyAwareComboBox();
		newComboBox.setTriggerAction(ComboBox.TriggerAction.ALL);
		newComboBox.setFieldLabel(label);
		newComboBox.setName(name);
		newComboBox.setStore(store);
		return newComboBox;
	}
	
	public DirtyAwareSimpleComboBox constructComboBox(String name, String label, List content){
		DirtyAwareSimpleComboBox newComboBox = new DirtyAwareSimpleComboBox();
		newComboBox.setTriggerAction(ComboBox.TriggerAction.ALL);
		newComboBox.setFieldLabel(label);
		newComboBox.setName(name);
		newComboBox.add(content);
		return newComboBox;
	}
	
	public DirtyAwareNumberField constructBigDecimalField(String name, String label){
		DirtyAwareNumberField newNumberField = new DirtyAwareNumberField();
		newNumberField.setPropertyEditor(new BigDecimalSupportingPropertyEditor());
//		newNumberField.setPropertyEditorType(BigDecimal.class);
		newNumberField.setFieldLabel(label);
		newNumberField.setName(name);
		newNumberField.setAllowNegative(false);
		newNumberField.setFormat(NumberFormat.getFormat("0.00"));
		return newNumberField;
	}
	
	public DirtyAwareNumberField constructIntegerField(String name, String label, int maxValue){
		DirtyAwareNumberField newNumberField = new DirtyAwareNumberField();
		newNumberField.setPropertyEditorType(Integer.class);
		newNumberField.setFieldLabel(label);
		newNumberField.setMaxValue(maxValue);
		newNumberField.setName(name);
		newNumberField.setAllowNegative(false);
		return newNumberField;
	}
	
	public DirtyAwareNumberField constructDecimalField(String name, String label){
		DirtyAwareNumberField newNumberField = new DirtyAwareNumberField();
		newNumberField.setPropertyEditorType(Double.class);
		newNumberField.setFieldLabel(label);
		newNumberField.setName(name);
		newNumberField.setAllowNegative(false);
		newNumberField.setFormat(NumberFormat.getFormat("0.00"));
		return newNumberField;
	}
	
	public DirtyAwareNumberField constructDecimalField(String name, String label, int maxValue){
		DirtyAwareNumberField newNumberField = new DirtyAwareNumberField();
		newNumberField.setPropertyEditorType(Double.class);
		newNumberField.setMaxValue(maxValue);
		newNumberField.setFieldLabel(label);
		newNumberField.setName(name);
		newNumberField.setAllowNegative(false);
		newNumberField.setFormat(NumberFormat.getFormat("0.00"));
		return newNumberField;
	}
	
	public DirtyAwareDateField constructDateField(String name, String label){
		DirtyAwareDateField newDateField = new DirtyAwareDateField();
		newDateField.setFieldLabel(label);
		newDateField.setName(name);
		return newDateField;
	}
	
	public DirtyAwareGrid constructDataGrid(ListStore store, ColumnModel cm){
		DirtyAwareGrid grid = new DirtyAwareGrid(store, cm);
		grid.setBorders(false);
		grid.setLoadMask(true);
        grid.setStripeRows(true);  
        grid.setColumnLines(true);  
        grid.setColumnReordering(true);
        grid.getView().setAutoFill(true);
        grid.getSelectionModel().setSelectionMode(Style.SelectionMode.SINGLE);
		return grid;
	}

    public DirtyAwareEditorGrid constructDataEditorGrid(ListStore store, ColumnModel cm){
        DirtyAwareEditorGrid grid = new DirtyAwareEditorGrid(store, cm);
		grid.setBorders(false);
		grid.setLoadMask(true);
        grid.setStripeRows(true);
        grid.setColumnLines(true);
        grid.setColumnReordering(true);
        grid.getView().setAutoFill(true);
        grid.getSelectionModel().setSelectionMode(Style.SelectionMode.SINGLE);
		return grid;
	}

}
